package com.ruoyi.business.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.TRegisterDept;
import com.ruoyi.business.domain.TRegisterInfo;
import com.ruoyi.business.domain.TRegisterLeave;
import com.ruoyi.business.domain.TRegisterNotice;
import com.ruoyi.business.enums.LeaveEffectiveStatusEnums;
import com.ruoyi.business.mapper.TRegisterInfoMapper;
import com.ruoyi.business.mapper.TRegisterLeaveMapper;
import com.ruoyi.business.service.ITRegisterDeptService;
import com.ruoyi.business.service.ITRegisterLeaveService;
import com.ruoyi.business.service.ITRegisterNoticeService;
import com.ruoyi.business.vo.TRegisterLeaveStatVo;
import com.ruoyi.business.vo.TRegisterLeaveVo;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 请假记录Service业务层处理
 *
 * @author Tellsea
 * @date 2024-02-06
 */
@Service
@RequiredArgsConstructor
public class TRegisterLeaveServiceImpl extends ServiceImpl<TRegisterLeaveMapper, TRegisterLeave> implements ITRegisterLeaveService {

    private final TRegisterInfoMapper registerInfoMapper;
    private final ITRegisterDeptService registerDeptService;
    private final ITRegisterNoticeService registerNoticeService;

    private Page<TRegisterLeaveVo> handleResult(Page<TRegisterLeaveVo> page) {
        page.getRecords().forEach(e -> handleResult(e));
        return page;
    }

    private List<TRegisterLeaveVo> handleResult(List<TRegisterLeaveVo> list) {
        list.forEach(e -> handleResult(e));
        return list;
    }

    private TRegisterLeaveVo handleResult(TRegisterLeaveVo vo) {
        vo.setEffectiveStatusName(LeaveEffectiveStatusEnums.getNameByCode(vo.getEffectiveStatus()));
        TRegisterInfo registerInfo = registerInfoMapper.selectById(vo.getRegisterId());
        vo.setUserName(registerInfo.getUserName());
        vo.setPhoneNumber(registerInfo.getPhoneNumber());
        TRegisterDept registerDept = registerDeptService.getById(registerInfo.getRegisterDeptId());
        vo.setRegisterDeptName(registerDept.getRegisterDeptName());
        TRegisterNotice registerNotice = registerNoticeService.queryByRegisterId(vo.getRegisterId());
        if (ObjectUtil.isNotEmpty(registerNotice)) {
            vo.setJobNumber(registerNotice.getJobNumber());
        }
        return vo;
    }

    @Override
    public TableDataInfo<TRegisterLeaveVo> queryList(TRegisterLeaveVo entity) {
        return PageUtils.buildDataInfo(handleResult(this.baseMapper.queryList(PageUtils.buildPage(), entity)));
    }

    @Override
    public List<TRegisterLeaveVo> queryAll(TRegisterLeaveVo entity) {
        return handleResult(this.baseMapper.queryList(entity));
    }

    @Override
    public TRegisterLeaveVo queryById(Long id) {
        return handleResult(this.baseMapper.queryById(id));
    }

    /**
     * 根据进修id，统计请假数据
     *
     * @param registerIdList
     * @return
     */
    @Override
    public List<TRegisterLeaveStatVo> getRegisterLeaveStat(List<Long> registerIdList) {
        if(registerIdList==null || registerIdList.isEmpty()){
            return Collections.emptyList();
        }
        return this.baseMapper.getRegisterLeaveStat(registerIdList);
    }
}
